package com.example.service.impl.ums;

import com.example.common.UUIDUtils;
import com.example.mapper.ums.RoleMenuMapper;
import com.example.model.ums.RoleMenu;
import com.example.service.ums.RoleMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 角色与菜单关联Service
 */
@Service
@Transactional
public class RoleMenuServiceImpl implements RoleMenuService {

    @Autowired
    private RoleMenuMapper mapper;


    @Override
    public List<RoleMenu> findRoleMenuByParam(RoleMenu roleMenu) {
        return mapper.findRoleMenuByParam(roleMenu);
    }

    @Override
    public void distribution(String roleId, List<String> menuIds) {
        //删除原来的绑定关系
        int i = mapper.deleteByRoleId(roleId);
        List<RoleMenu> roleMenus = new ArrayList<>();
        //删除成功绑定信息关系
        if (menuIds.size() == 0) {
            return;
        }
        for (String item : menuIds) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setId(UUIDUtils.getId());
            roleMenu.setMenuId(item);
            roleMenu.setRoleId(roleId);
            roleMenus.add(roleMenu);
        }
        ;
        mapper.insertBatch(roleMenus);

    }

    @Override
    public int insert(RoleMenu roleMenu) {
        roleMenu.setId(UUIDUtils.getId());
        return mapper.insert(roleMenu);
    }
}
